;(function(window,$){
    var infobj=null;

    $(function(){
        
        getinfo();

        //when a file upload,get the oss url to hidden input

        $('input[type="file"]').on('change',function(){
            var fileInput=$(this);
            
            util.fileUpload(fileInput[0].files[0],function(res){
                fileInput.prev('span').text('点击重新上传');
                fileInput.next('input[type="hidden"]').val(res.data);
            })
        });

        $('.uploadbox').hover(function(){
            var imgUrl=$(this).find("input[type='hidden']").val();
            if (imgUrl!=="")
            {
                $(this).find("img").attr("src",imgUrl);
                $(this).find("a").attr("href",imgUrl).show();;
            }
        },function(){
            $(this).find("a").hide();
        });

        //dialog
        $(".exitId-example").on('click',function(){
            $('.'+$(this).data('dialog')).show();
        });

        $('.dialog').on('click',function(){
            $(this).hide();
        });

        $('#save').on('click',saveinfo);

    });

    function saveinfo(){

        if(!submitCheck())
        {
            return;
        }

        infobj.idCard=$('input[name="idCard"]').val();

        // all hidden,if a hidden is empty,do not let it edit infobj
        $('input[type="hidden"]').each(function(){
            var val=$(this).val();
            if (!val){return;}
            var key=$(this).attr('name');
            infobj[key]=val;
        });

        // field to del
        
        delete infobj.id
        delete infobj.status
        delete infobj.userType
        infobj.telPhone=infobj.phone
        delete infobj.phone
        delete infobj.birthday
        delete infobj.score
        delete infobj.successCount
        delete infobj.auditStatus

        util.ajax({
            url:'/manager/profile/save',
            type:'POST',
            data:infobj,
            success:function(res){
                console.log(res);
                window.location.href="/channel/myprofile.html";
            }
        })
    }

    function getinfo(){
        util.ajax({
            type:'GET',
            url:'/user/manager',
            success:function(res){
                infobj=res.data
                $('input[name="idCard"]').val(infobj.idCard);
                $('input[name="idCardFront"]').val(infobj.idCardFront);
                $('input[name="idCardVerso"]').val(infobj.idCardVerso);
                $('input[name="idCardPhoto"]').val(infobj.idCardPhoto);

                $('span#user-name').text(infobj.name);
                $('span#user-company').text(infobj.companyName);
                $('span#profile-status').text(parseInt(infobj.isProfile)===1?'已认证':'未认证');
                $('img#headshow').attr('src',infobj.avatar);
            }
        })
    }

    function submitCheck(){
        if ($("input[name='idCard']").val()==='')
        {
            $("input[name='idCard']").css("border-color","#FF0000");
            return false
        };

        return true;
    }

})(window,jQuery)